<template>
  <div class="admin-login">
    <div class="login-container">
      <div class="login-card">
        <div class="login-header">
          <h1>管理员登录</h1>
          <p>请输入管理员密码以访问管理功能</p>
        </div>
        
        <el-form 
          :model="loginForm" 
          :rules="rules" 
          ref="loginFormRef"
          @submit.prevent="handleLogin"
          class="login-form"
        >
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入管理员密码"
              size="large"
              show-password
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              size="large" 
              @click="handleLogin"
              :loading="loading"
              class="login-button"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-footer">
          <el-button 
            type="text" 
            @click="showPasswordInfo = true"
            class="password-info-btn"
          >
            忘记密码？
          </el-button>
        </div>
      </div>
    </div>
    
    <!-- 密码信息对话框 -->
    <el-dialog
      v-model="showPasswordInfo"
      title="管理员密码信息"
      width="500px"
      :close-on-click-modal="false"
    >
      <div class="password-info">
        <el-alert
          title="当前管理员密码"
          type="info"
          :closable="false"
          show-icon
        >
          <template #default>
            <div class="password-display">
              <code>{{ currentPassword }}</code>
              <el-button 
                type="primary" 
                size="small" 
                @click="copyPassword"
                class="copy-btn"
              >
                复制
              </el-button>
            </div>
          </template>
        </el-alert>
        
        <div class="password-actions">
          <el-button 
            type="warning" 
            @click="handleRegeneratePassword"
            :loading="regenerating"
          >
            重新生成密码
          </el-button>
        </div>
        
        <el-alert
          title="注意"
          type="warning"
          :closable="false"
          show-icon
          class="warning-alert"
        >
          <template #default>
            <p>重新生成密码后，当前登录状态将被清除，需要重新登录。</p>
            <p>请妥善保管管理员密码，避免泄露。</p>
          </template>
        </el-alert>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { adminLogin, getCurrentPassword, regeneratePassword } from '../utils/auth'

export default {
  name: 'AdminLogin',
  components: {
    Lock
  },
  setup() {
    const router = useRouter()
    const loginFormRef = ref()
    const loading = ref(false)
    const showPasswordInfo = ref(false)
    const regenerating = ref(false)
    const currentPassword = ref('')
    
    const loginForm = reactive({
      password: ''
    })
    
    const rules = {
      password: [
        { required: true, message: '请输入管理员密码', trigger: 'blur' }
      ]
    }
    
    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      try {
        await loginFormRef.value.validate()
        loading.value = true
        
        const success = await adminLogin(loginForm.password)
        if (success) {
          // 登录成功后跳转到管理员页面
          router.push({ name: 'AdminPanel' })
        }
      } catch (error) {
        console.error('登录验证失败:', error)
      } finally {
        loading.value = false
      }
    }
    
    const copyPassword = () => {
      navigator.clipboard.writeText(currentPassword.value).then(() => {
        ElMessage.success('密码已复制到剪贴板')
      }).catch(() => {
        ElMessage.error('复制失败')
      })
    }
    
    const handleRegeneratePassword = async () => {
      regenerating.value = true
      try {
        const newPassword = await regeneratePassword()
        if (newPassword) {
          currentPassword.value = newPassword
        }
      } catch (error) {
        ElMessage.error('重新生成密码失败')
      } finally {
        regenerating.value = false
      }
    }
    
    onMounted(() => {
      currentPassword.value = getCurrentPassword()
    })
    
    return {
      loginForm,
      rules,
      loginFormRef,
      loading,
      showPasswordInfo,
      regenerating,
      currentPassword,
      handleLogin,
      copyPassword,
      handleRegeneratePassword
    }
  }
}
</script>

<style scoped>
.admin-login {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  color: #333;
  margin-bottom: 10px;
  font-size: 28px;
  font-weight: bold;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  font-weight: bold;
}

.login-footer {
  text-align: center;
}

.password-info-btn {
  color: #666;
  font-size: 14px;
}

.password-info {
  padding: 20px 0;
}

.password-display {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
}

.password-display code {
  background: #f5f5f5;
  padding: 8px 12px;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  flex: 1;
  word-break: break-all;
}

.copy-btn {
  flex-shrink: 0;
}

.password-actions {
  margin: 20px 0;
  text-align: center;
}

.warning-alert {
  margin-top: 20px;
}

.warning-alert p {
  margin: 5px 0;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }
  
  .login-header h1 {
    font-size: 24px;
  }
}
</style>
